package org.example.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.example.entity.*;
import org.example.form.SearchForm;
import org.example.mapper.*;
import org.example.service.AbsentService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.example.vo.AbsentVO;
import org.example.vo.PageVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author admin
 * @since 2024-10-31
 */
@Service
public class AbsentServiceImpl extends ServiceImpl<AbsentMapper, Absent> implements AbsentService {
   @Autowired
   private AbsentMapper absentMapper;
   @Autowired
   private DormitoryMapper dormitoryMapper;
   @Autowired
   private StudentMapper studentMapper;
   @Autowired
   private BuildingMapper buildingMapper;
   @Autowired
   private DormitoryAdminMapper dormitoryAdminMapper;
    @Override
    public PageVO list(Integer page, Integer size) {
        Page<Absent> absentPage = new Page<>(page, size);
        Page<Absent> absentPage1 = absentMapper.selectPage(absentPage, null);
        List<AbsentVO> absentVOList = new ArrayList<>();
        for (Absent record : absentPage1.getRecords()) {
            AbsentVO absentVO = new AbsentVO();
            BeanUtils.copyProperties(record, absentVO);
            absentVO.setBuildingName(buildingMapper.selectById(record.getBuildingId()).getName());
            absentVO.setStudentName(studentMapper.selectById(record.getStudentId()).getName());
            absentVO.setDormitoryName(dormitoryMapper.selectById(record.getDormitoryId()).getName());
            absentVO.setDormitoryAdminName(dormitoryAdminMapper.selectById(record.getDormitoryAdminId()).getName());
            absentVOList.add(absentVO);
        }
        PageVO pageVO = new PageVO();
        pageVO.setTotal(absentPage1.getTotal());
        pageVO.setData(absentVOList);
        return pageVO;
    }

    @Override
    public PageVO search(SearchForm searchForm) {
        Page<Absent> absentPage = new Page<>(searchForm.getPage(), searchForm.getSize());
        Page<Absent> absentPage1 = null;
        if(searchForm.getValue().equals("")){
            absentPage1 = absentMapper.selectPage(absentPage, null);


        }else if(searchForm.getKey().equals("buildingName")){
            QueryWrapper<Building> buildingQueryWrapper = new QueryWrapper<>();
            QueryWrapper<Absent> absentQueryWrapper = new QueryWrapper<>();
            buildingQueryWrapper.like("name", searchForm.getValue());
            List<Building> buildingList = this.buildingMapper.selectList(buildingQueryWrapper);
            List<Integer> idList = new ArrayList<Integer>();
            for (Building building : buildingList) {
                idList.add(building.getId());
            }
            absentQueryWrapper.in("building_id", idList);
            absentPage1 = absentMapper.selectPage(absentPage, absentQueryWrapper);
        }else if(searchForm.getKey().equals("dormitoryName")){
            QueryWrapper<Absent> absentQueryWrapper = new QueryWrapper<>();
            QueryWrapper<Dormitory> dormitoryQueryWrapper = new QueryWrapper<>();
            dormitoryQueryWrapper.like("name", searchForm.getValue());
            List<Dormitory> dormitoryList = this.dormitoryMapper.selectList(dormitoryQueryWrapper);
            List<Integer> idList = new ArrayList<Integer>();
            for (Dormitory dormitory : dormitoryList) {
                idList.add(dormitory.getId());
            }
            absentQueryWrapper.in("dormitory_id", idList);
            absentPage1 = absentMapper.selectPage(absentPage, absentQueryWrapper);
        }
        List<AbsentVO> absentVOList = new ArrayList<>();
        for (Absent record : absentPage1.getRecords()) {
            AbsentVO absentVO = new AbsentVO();
            BeanUtils.copyProperties(record, absentVO);
            absentVO.setBuildingName(buildingMapper.selectById(record.getBuildingId()).getName());
            absentVO.setStudentName(studentMapper.selectById(record.getStudentId()).getName());
            absentVO.setDormitoryName(dormitoryMapper.selectById(record.getDormitoryId()).getName());
            absentVO.setDormitoryAdminName(dormitoryAdminMapper.selectById(record.getDormitoryAdminId()).getName());
            absentVOList.add(absentVO);
        }
        PageVO pageVO = new PageVO();
        pageVO.setTotal(absentPage1.getTotal());
        pageVO.setData(absentVOList);
        return pageVO;
    }
}
